package com.admin.controller;

import com.admin.service.AdminService;
import com.admin.util.CommonUtil;
import com.admin.util.LogUtil;
import com.admin.util.Result;
import com.data.entity.Admin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @描述 <>
 * @参数 $params$
 * @返回值 $return$
 * @创建人 WangGuanghua
 * @创建时间 $date$
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员")
public class AdminController {
    private Logger logger = LogUtil.getBussinessLogger();

    @Resource
    private AdminService adminService;



    @GetMapping("/list")
    @ApiOperation("管理员：分页列表")
    public Result list(Integer limit, Integer page) {
        Result result = new Result();
        try {
            List<Admin> adminDTOList = adminService.listPage(page, limit);
            result.setCode(Result.Code.SUCCESS);
            result.setData(adminDTOList);
            result.setCount(adminService.count());
        } catch (Exception e) {
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }





    /**
     * 删除管理员信息
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation("管理员：删除")
    public Result delete(Long id){
        Result result=new Result();
        try {
            adminService.delete(id);
            result.setCode(Result.Code.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            result.setCode(Result.Code.SYSTEMERROR);
        }
        return result;
    }

    @GetMapping("/myInfo")
    @ApiOperation("管理员：信息")
    public Result getMyInfo(Long id) {
        Result result = new Result();
        try {
            Admin admin = adminService.getById(id);
            result.setCode(Result.Code.SUCCESS);
            result.setData(admin);
        } catch (Exception e) {
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }


    @PostMapping("/addOrEdit")
    @ApiOperation("管理员：添加/修改")
    public Result addOrEdit(Admin admin) {
        Result result = new Result();
        try {
            if (!CommonUtil.isEmpty(admin.getId())) {
                //修改信息
                Admin editAdmin = adminService.getById(admin.getId());
                editAdmin.setGender(admin.getGender());
                editAdmin.setUpdatetime(new Date());
                editAdmin.setRealname(admin.getRealname());
                editAdmin.setUsername(admin.getUsername());
                editAdmin.setPhone(admin.getPhone());
                editAdmin.setRoleId(admin.getRoleId());
                editAdmin.setStatus(admin.getStatus());
                adminService.edit(editAdmin);
                result.setMessage(Result.Code.SUCCESS, "操作成功！");
            } else {
                //添加信息
                Admin admin1 = adminService.getByUsername(admin.getUsername()); //根据登录名查询
                if (CommonUtil.isEmpty(admin1)) {
                    //判断用户名不冲突后执行添加操作
                    logger.info("================================"+admin.getUsername());
                    adminService.add(admin);
                    result.setMessage(Result.Code.SUCCESS, "添加成功！初始密码为：000000");
                } else if (!CommonUtil.isEmpty(admin1)){
                    result.setMessage(Result.Code.AUTHERROR, "该登录名已存在，请勿重复添加！");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }




}
